<?php

include 'Model.php';

class User extends Model {

    private $table = "users";
    private $id = 0;
    private $name = "";
    private $email = "";
    private $login = "";
    private $password = "";

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getLogin() {
        return $this->login;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function setLogin($login) {
        $this->login = $login;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function isVadatePasssWord($confirmPassword) {
        return $confirmPassword == $this->password;
    }

    public function insert() {
        $this->password = md5($this->password);
        $query = "insert into $this->table(name,email,login,password) "
                . "values('$this->name','$this->email','$this->login','$this->password')";
        if (mysql_query($query) >= 1) {
            $user = new User();
            return true;
        } else {
            return false;
        }
    }

    public function delete($id) {
        $query = "delete from $this->table where id = $id";
        if (mysql_affected_rows($query)) {
            return true;
        } else {
            return false;
        }
    }

    public function update() {
        $this->password = md5($this->password);
        $query = "update $this->table set name = '$this->name',"
                . "email = '$this->email',login = '$this->login',password = '$this->password' where id = $this->id";
        if (mysql_query($query) >= 1) {
            $user = new User();
            return true;
        } else {
            return false;
        }
    }

    public function selectById($id) {
        $query = "SELECT * from $this->table where id = " + $id;
        $result = mysql_query($query);
        return mysql_fetch_object($result);
    }

    public function validAuthentication($login, $password) {
        $password = md5($password);
        $query = "SELECT * from $this->table where login = '$login' and password = '$password'";
        $result = mysql_query($query);
        $object = mysql_fetch_object($result);
        if ($object != null) {
            $this->setId($object->id);
            $this->setName($object->name);
            $this->setEmail($object->email);
            $this->setLogin($object->login);
            return true;
        }
        return false;
    }

}

?>
